﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
using UnityEngine.EventSystems;

// 选人界面
public class SelectRole : MonoBehaviour {
    GameObject _contentObj;
    Button _BtnEnter;
    Button _BtnCreate;
    Touch _RotateTouch; // 旋转脚本


    GameObject _modelStudioRoot;
    GameObject _modelPlaceObj;

    int curSelectIndex = -1; // 当前选中的角色索引号

    GameObject _curModelObj;

    Quaternion _srcModelRotation; // 原始模型节点朝向角度

    // 假的角色数据
    List<NCmd.RoleSelect> allRoleList;

	// Use this for initialization
	void Awake ()
    {
        allRoleList = SelectRoleMgr.Instance.GetAllRoleList();

        _contentObj = gameObject.FindComponent<Transform>("Scroll View/Viewport/Content").gameObject;
        _BtnEnter = gameObject.FindComponent<Button>("BtnEnter");
        _BtnCreate = gameObject.FindComponent<Button>("BtnCreate");
        _BtnEnter.onClick.AddListener(onBtnEnterClick);
        _RotateTouch = gameObject.FindComponent<Touch>("Touch");

        // 摄影棚相关
        _modelStudioRoot = ResMgr.Instance.GetGameObject("Prefabs/UI/SelectRole/ModelStudio");
        _modelPlaceObj = _modelStudioRoot.FindComponent<Transform>("ModelPlace").gameObject;
        _srcModelRotation = _modelPlaceObj.transform.rotation;
        // 触摸旋转控制的节点
        _RotateTouch.OnDragCallback = rotateObj;


        var group = _contentObj.GetComponent<ToggleGroup>();
        // 显示已创建角色列表

        // 默认选中第N个
        int defaultSelectIndex = 0;

        var Template = ResMgr.Instance.LoadTemplate<GameObject>("Prefabs/UI/SelectRole/RoleItem");
        int index = 0;
        foreach (var role in allRoleList)
        {
            // 创建toggleItem
            Toggle toggle = GameObject.Instantiate(Template).GetComponent<Toggle>();
            var nameLabel = toggle.gameObject.FindComponent<Text>("Label");
            nameLabel.text = role.Name;

            toggle.transform.SetParent(_contentObj.transform, false);
            toggle.group = group;

            var curIndex = index;
            toggle.onValueChanged.AddListener((isOn) => OnToggleValueChanged(curIndex, isOn));

            toggle.isOn = (index == defaultSelectIndex);

            index++;
        }

    }

    // 界面被销毁时
    private void OnDestroy()
    {
        ResMgr.Instance.Release(_modelStudioRoot);
    }

    private void onBtnEnterClick()
    {
        // 实际上应该在mgr里面发送消息
        NCmd.EnterScene cmd = new NCmd.EnterScene();
        cmd.Index = curSelectIndex;

        Net.Instance.SendCmd(cmd);
    }
    
    private void OnToggleValueChanged(int index, bool isOn)
    {
        if (isOn)
        {
            if (curSelectIndex == index)
            {
                return;
            }

            curSelectIndex = index;
            // 展示模型
            var roleInfo = allRoleList[index];
            
            // 清空摄影棚节点
            _modelPlaceObj.DestroyAllChildren();

            // 还原旋转角度
            _modelPlaceObj.transform.rotation = _srcModelRotation;

            var modelPath = RoleTable.Instance[roleInfo.ModelID].ModelPath;
            _curModelObj = ResMgr.Instance.GetGameObject(modelPath);

            _curModelObj.transform.SetParent(_modelPlaceObj.transform, false);
        }
    }

    // 旋转模型节点
    private void rotateObj(PointerEventData eventData)
    {
        _modelPlaceObj.transform.Rotate(new Vector3(0, -eventData.delta.x, 0));
    }
}
